package com.anthill.events.hosts
{
import com.anthill.controllers.EventBus;
import com.anthill.events.AnthillEvent;
import com.anthill.namespaces.anthill;

import flash.events.Event;

[DefaultProperty("children")]
/**
 *  Listener contain anthill events and 
 *  provide method to add listeners for them.
 * 
 */ 
public class Listener extends EventBus
{
	include "../../Version.as";
	
	use namespace anthill;
	
	/**
     *  Constructor. 
     *  
     */ 
	public function Listener()
	{
	}
	
	/**
     *  @private
     *  Storage for children property.
     */	
    private var _children:Array = [];
    
    [Inspectable(category="General", arrayType="com.anthill.events.AnthillEvent")]
    [Bindable("childrenChanged")]
	 /**
     *  Array of anthill events.
     *
     *  @default false
     */ 
    public function get children():Array
    {
        return _children;
    }
    
    [ArrayElementType("com.anthill.events.AnthillEvent")] 
    /**
     *  @private
     */
    public function set children(value:Array):void
    {
		_children = value;
		for each (var event:AnthillEvent in _children)
		{
			event.parent = this;	
		}
		startListening();
		dispatchEvent(new Event("childrenChanged"));
	}
	

	/**
     *  Start listening AnthillEvents.
     */
	public function startListening():void
	{
		for each(var event:AnthillEvent in _children)
		{
			event.startListening();
		}
	}
	
	/**
     *  Stop listening AnthillEvents.
     */
	public function stopListening():void
	{
		for each(var event:AnthillEvent in _children)
		{
			event.stopListening();
		}
	}
	
}
}